<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.user_id"
        placeholder="会员ID"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-button
        v-waves
        type="primary"
        icon="el-icon-search"
        class="filter-item"
        style="margin-left: 10px;"
        @click="handleFilter"
      >搜 索</el-button>
      <el-button
        v-waves
        type="success"
        icon="el-icon-refresh"
        class="filter-item"
        style="margin-left: 10px;"
        @click="clearFilter"
      >清除筛选</el-button>
    </div>
    <div class="filter-container">
      <el-button
        v-waves
        type="success"
        icon="el-icon-edit"
        class="filter-item"
        size="small"
        @click="handleBatchCheck"
      >批量审核</el-button>
      <el-button
        v-waves
        type="danger"
        icon="el-icon-edit"
        class="filter-item"
        size="small"
        @click="handleBatchReject"
      >批量驳回</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :border="false"
      fit
      style="width: 100%;"
      @selection-change="selectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
      />
      <el-table-column label="ID" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员ID">
        <template slot-scope="{row}">
          <span>{{ row.user_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="昵称">
        <template slot-scope="{row}">
          <span>{{ row.user.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机">
        <template slot-scope="{row}">
          <span>{{ row.user.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" width="120">
        <template slot-scope="{row}">
          <div class="demo-image__lazy" v-if="row.path && row.path.length > 0">
            <el-image :src="row.path[0]" lazy :fit="'cover'" :preview-src-list="row.path">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评论" show-overflow-tooltip width="200">
        <template slot-scope="{row}">
          <span>{{ row.content | contentText }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <span>{{ row.check | statusText }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评论时间" sortable="custom" prop="create_time" width="160">
        <template slot-scope="{row:{create_time}}">
          <i class="el-icon-time" />
          <span style="margin-left: 5px">{{ create_time | timeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="250"
        fixed="right"
      >
        <template slot-scope="{row}">
          <el-button v-if="+row.check !== 1" type="success" size="mini" icon="el-icon-edit" @click="handleCheck(row)">
            审核
          </el-button>
          <el-button v-if="+row.check !== -1" type="warning" size="mini" icon="el-icon-edit" @click="handleReject(row)">
            驳回
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      :page-sizes="[5, 10, 20, 50, 100]"
      @pagination="refresh"
    />
  </div>
</template>

<script>
import { parseTime } from '@/utils'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import api from '@/api'

const initQuery = {
  page: 1,
  pageSize: 10,
  total: 0,
}

export default {
  name: 'OrderList',
  components: {
    Pagination,
  },
  directives: { waves },
  filters: {
    timeFilter(time) {
      return time ? parseTime(time, '{m}-{d} {h}:{i}') : ''
    },
    statusText(status) {
      const v = +status
      let text = ''
      if (v === 0) {
        text = '待审核'
      }
      if (v === -99) {
        text = '已删除'
      }
      if (v === 1) {
        text = '通过'
      }
      if (v === -1) {
        text = '已驳回'
      }
      return text
    },
    contentText(v) {
      return v.substring(0, 10000)
    }
  },
  data() {
    return {
      listLoading: false,
      list: [],
      listQuery: {},
      selectionList: [],
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getList()
  },
  methods: {
    selectionChange(data) {
      this.selectionList = data
    },
    handleBatchCheck() {
      if (this.selectionList.length < 1) {
        this.$notify({
          type: 'error',
          title: '错误',
          message: '请选择要批量审核的数据',
          duration: 2000
        })
      } else {
        this.$confirm('您确定要审核这些吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const ids = this.selectionList.map(x => x.id)
          api.comment.batchCheck({ ids }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '审核成功',
              type: 'success',
              duration: 2000
            })
            this.listQuery.d = new Date().getTime()
            this.handleFilter()
          })
        })
      }
    },
    handleBatchReject() {
      if (this.selectionList.length < 1) {
        this.$notify({
          type: 'error',
          title: '错误',
          message: '请选择要批量驳回的数据',
          duration: 2000
        })
      } else {
        this.$confirm('您确定要驳回这些吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const ids = this.selectionList.map(x => x.id)
          api.comment.batchReject({ ids }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '驳回成功',
              type: 'success',
              duration: 2000
            })
            this.listQuery.d = new Date().getTime()
            this.handleFilter()
          })
        })
      }
    },
    handleCheck(row) {
      this.$confirm('您确定要审核吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.comment.check({ id: row.id }).then(response => {
          this.$message({
            message: response.message || '审核成功',
            type: 'success'
          })
          this.list.forEach(x => {
            if (x.id === row.id) {
              x.check = 1
            }
          })
        })
      }).catch(() => {})
    },
    handleReject(row) {
      this.$confirm('您确定要驳回吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.comment.check({ id: row.id }).then(response => {
          this.$message({
            message: response.message || '驳回成功',
            type: 'success'
          })
          this.list.forEach(x => {
            if (x.id === row.id) {
              x.check = -1
            }
          })
        })
      }).catch(() => {})
    },
    /**
     * 获取数据列表
     */
    getList() {
      this.listLoading = true
      api.comment.list({ ...this.listQuery, id: this.$route.params.id }).then(response => {
        const { data } = response
        this.list = data.list
        this.listQuery.total = +data.totalCount
        this.listLoading = false
      }).finally(() => {
        this.listLoading = false
      })
    },
    /**
     * 刷新
     */
    refresh() {
      this.$router.push({
        path: `/order/comment/${this.$route.params.id}`,
        query: this.listQuery
      })
    },
    /**
     * 过滤(筛选)
     */
    handleFilter() {
      // this.listQuery.page = 1
      this.refresh()
    },
    /**
     * 清除筛选
     */
    clearFilter() {
      this.listQuery = Object.assign({}, initQuery)
      this.refresh()
    },
    /**
     * 处理筛选条件
     */
    parseQuery() {
      const query = Object.assign({}, this.$route.query)
      this.listQuery = Object.assign({}, { ...initQuery })
      if (Object.keys(query).length > 0) {
        // 注意这里的类型转换
        query.page && (query.page = +query.page)
        query.pageSize && (query.pageSize = +query.pageSize)
        query.total && (query.total = +query.total)
      }
      this.listQuery = { ...this.listQuery, ...query }
    }
  },
  /**
   * 刷新
   * @param to
   * @param from
   * @param next
   */
  beforeRouteUpdate(to, from, next) {
    if (to.path === from.path) {
      const newQuery = Object.assign({}, to.query)
      const oldQuery = Object.assign({}, from.query)
      if (JSON.stringify(newQuery) !== JSON.stringify(oldQuery)) {
        this.getList()
      }
    }
    next()
  }
}
</script>

<style scoped lang="scss">
.el-divider--horizontal {
  margin: 5px 0;
}
</style>
